package com.jcfk.eam.service.mould;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.mould.EamMouldPackageRecordDao;
import com.jcfk.eam.domain.dto.mould.EamMouldPackageRecordDTO;
import com.jcfk.eam.domain.po.main.EamAssetInfo;
import com.jcfk.eam.domain.po.mould.EamMouldPackageRecord;
import com.jcfk.eam.domain.po.mould.EamMouldTaskInfo;
import com.jcfk.eam.domain.po.mould.EamMouldTaskInfoDetail;
import com.jcfk.eam.domain.query.mould.EamMouldPackageRecordQuery;
import com.jcfk.eam.enums.MouldPackageRecordStatusEnum;
import com.jcfk.eam.service.main.EamAssetInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 模板框记录
 *
 * @author ly
 * @email 522891237@gqq.com
 * @date 2025-08-21
 */
@Service
public class EamMouldPackageRecordService extends BaseService<EamMouldPackageRecordDao, EamMouldPackageRecord> {
    @Autowired
    private EamAssetInfoService eamAssetInfoService;

    /**
     * EamMouldPackageRecord分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamMouldPackageRecordDTO> queryByPage(DataPage<EamMouldPackageRecordDTO> page, EamMouldPackageRecordQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamMouldPackageRecord是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamMouldPackageRecordDTO dto) {
        QueryWrapper<EamMouldPackageRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入模板框记录
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamMouldPackageRecordDTO dto) {

        // 保存
        EamMouldPackageRecord item = new EamMouldPackageRecord();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新模板框记录
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamMouldPackageRecordDTO dto) {

        // 保存
        EamMouldPackageRecord item = new EamMouldPackageRecord();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除模板框记录(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamMouldPackageRecordDTO dto) {

        EamMouldPackageRecord item = new EamMouldPackageRecord();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamMouldPackageRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamMouldPackageRecord item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取模板框记录信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamMouldPackageRecordDTO get(String id) {
        // 查询数据
        EamMouldPackageRecord item = this.baseMapper.selectById(id);
        EamMouldPackageRecordDTO dto = new EamMouldPackageRecordDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMouldPackageRecordDTO>
     */
    public List<EamMouldPackageRecordDTO> getList(EamMouldPackageRecordQuery query) {
        // 查询条件
        QueryWrapper<EamMouldPackageRecord> qw = new QueryWrapper<>();
        // 查询数据
        List<EamMouldPackageRecord> list = super.list(qw);

        return convertList(list, EamMouldPackageRecordDTO.class);
    }

    /**
     * 根据主键获取模板框记录信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        EamMouldPackageRecord item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }

    /**
     * 批量插入
     *
     * @param detailList
     * @param outbound
     * @param eamMouldTaskInfo
     */
    public void insertBatch(List<EamMouldTaskInfoDetail> detailList, MouldPackageRecordStatusEnum outbound, EamMouldTaskInfo eamMouldTaskInfo) {
        if (detailList != null && !detailList.isEmpty()) {
            QueryWrapper<EamAssetInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(EamAssetInfo::getDeleted, 0);
            queryWrapper.lambda().eq(EamAssetInfo::getAssetKind, 3);
            queryWrapper.lambda().in(EamAssetInfo::getAssetCode, detailList.stream().map(EamMouldTaskInfoDetail::getMouldCode).collect(Collectors.toList()));
            List<EamAssetInfo> eamAssetInfoList = eamAssetInfoService.list(queryWrapper);
            if (!eamAssetInfoList.isEmpty()) {
                List<EamMouldPackageRecord> eamMouldPackageRecordList = eamAssetInfoList.stream()
                        .filter(item -> StrUtil.isNotEmpty(item.getMouldPackageId()))
                        .map(item -> {
                            EamMouldPackageRecord eamMouldPackageRecord = new EamMouldPackageRecord();
                            this.setDefaultValue(eamMouldPackageRecord);
                            eamMouldPackageRecord.setMouldPackageId(item.getMouldPackageId());
                            eamMouldPackageRecord.setMouldTaskId(eamMouldTaskInfo.getId());
                            eamMouldPackageRecord.setMouldCode(item.getAssetCode());
                            eamMouldPackageRecord.setMouldName(item.getAssetName());
                            eamMouldPackageRecord.setStatus(outbound.getCode());
                            return eamMouldPackageRecord;
                        }).collect(Collectors.toList());
                if (!eamMouldPackageRecordList.isEmpty()) {
                    this.saveBatch(eamMouldPackageRecordList);
                }
            }
        }

    }
}

